const utils = require("../utils/index.js");
const tools = require("../utils/tools")
const mq = require("../utils/mqInit")
const { v4: uuidv4 } = require('uuid');
const cryptoTool = require('../utils/cryptoTool')
const {logger} = require('../utils/logger')
const getMethodAccess = async (req, res) => {
    let query=req.query;
    try{
        const decodedData = cryptoTool.decryptWithPrivateKey(decodeURIComponent(query.data))
        const parsedData = JSON.parse(decodedData);

        // if(parsedData.event && parsedData.event !== 'custom'){
        //     // 不是自定义事件 不处理
        //    return  res.send(utils.returnData({data: {
        //             id:-1,
        //    }}));
        // }
        const reqIp = tools.getClientIp(req)
        let  clientId = req.cookies.clientId || uuidv4()
        const referer = req.headers.referer || req.headers.referrer
        const params = {
            ...parsedData,
            reqIp,
            reqToken:req.headers['token'],
            lineType:req.headers['lineType'] || req.headers['linetype'],
            cityName:req.headers['cityName'] || req.headers['cityname'],
            loginDevice:req.headers['loginDevice'] || req.headers['logindevice'],
            userAgent:req.headers['user-agent'],
            clientId,
            logId:uuidv4(),
            domain:referer
        }
        const r = await mq.send(JSON.stringify(params)).catch((e) => {
            console.log('生产者发送错误:' + e)
            return res.send(utils.returnData({code: -1,err:'系统出错',req}));
        })
        logger.reqNum({
            method:'get',
            msg:'请求成功，发送完成',
        })
        res.setCookie('clientId', params.clientId, { httpOnly: true, maxAge: 365 * 24 * 60 * 60, path: '/' });
        res.send(utils.returnData({data: {
                id:params.logId,
            }}));

    }catch (e) {
        console.log(e,'trackas-e')
        return res.send(utils.returnData({code: -1,err:'系统出错',req}));
    }
}

const postMethodAccess = async (req, res) => {
    let query=req.body;
    try{
        // const decodedData = tools.base64Decode(query.data);
        const decodedData = cryptoTool.decryptWithPrivateKey(decodeURIComponent(query.data))
        const parsedData = JSON.parse(decodedData);

        // console.log(parsedData,'解密后的');
        // if(parsedData.event && parsedData.event !== 'custom'){
        //     // 不是自定义事件 不处理
        //    return  res.send(utils.returnData({data: {
        //             id:-1,
        //    }}));
        // }
        const reqIp = tools.getClientIp(req)
        let  clientId = req.cookies.clientId || uuidv4()
        const referer = req.headers.referer || req.headers.referrer
        const params = {
            ...parsedData,
            reqIp,
            reqToken:req.headers['token'],
            lineType:req.headers['lineType'] || req.headers['linetype'],
            cityName:req.headers['cityName'] || req.headers['cityname'],
            loginDevice:req.headers['loginDevice'] || req.headers['logindevice'],
            userAgent:req.headers['user-agent'],
            clientId,
            logId:uuidv4(),
            domain:referer
        }
        const r = await mq.send(JSON.stringify(params)).catch((e) => {
            return res.send(utils.returnData({code: -1,err:'系统出错',req}));
        })
        logger.reqNum({
            method:'post',
            msg:'请求成功，发送完成',
        })
        res.setCookie('clientId', params.clientId, { httpOnly: true, maxAge: 365 * 24 * 60 * 60, path: '/' });
        res.send(utils.returnData({data: {
                id:params.logId,
            }}));

    }catch (e) {
        console.log(e,'trackas-e')
        return res.send(utils.returnData({code: -1,err:'系统出错',req}));
    }
}
module.exports = {
    getMethodAccess,
    postMethodAccess
}